<?

if ($DATA['component']['id']) {

	// list editor
	
	$_id_component	= $DATA['component']['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value

	$_crit = "id_data=$_id_data AND id_parent=$_id_component";

	$_param			= 'edit';				//	ID param name in the URL, used here & @ template
	$_multilang			= array('title');	// which fields are stored in language variations
	$_singlelang		= array('year', 'month', 'cat');	// which fields are stored without language variations
	

	$s = array_extend( array(
		'path' => 'project', 'w' => 128, 'h' => 96, 'param' => 'id', 'sw' => 640, 'sh' => 390
	), $DATA['component']['settings']);


	$s['path'] = createdir(FP_UPLOAD_PATH."$s[path]/");
	
	if (($a = (int)param($_param)) && ($DATA['object'] = datafetch('db_item', tb('data'), "$_crit AND id=$a"))) {


		$_cr2 = "id_data=$_id_data AND id_parent=$a";


		// tasks handling
	
		if ($sl = (int)param('delete')) {
	
			if ($p = $DATA['component']['settings']['path']) {
				killImage(FP_UPLOAD_PATH."$p/$sl");
			}
	
			dataset('db_delete', tb('data'), "id_data=$_id_meta AND id_parent=$sl");
			dataset('db_delete', tb('data'), "$_cr2 AND id=$sl");
		
	//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
			exit;
		}
		
		if ($sl = (int)param('show')) {
	
			dataset('db_update', tb('data'), "attr = attr | ".AA_VISIBLE, "$_cr2 AND (id=$sl)");
		
	//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'show')));
			exit;
		}
		
		if ($sl = (int)param('hide')) {
			
			dataset('db_update', tb('data'), "attr = attr & ~".AA_VISIBLE, "$_cr2 AND (id=$sl)");
		
	//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'hide')));
			exit;
		}
		
		
		if ($sort = strtolower(trim(param('sort')))) {
			
			switch($sort) {
			
				case 'manual':	// ajax request, no data should be returned
		
					$t = 1;
					
					parse_str(postdata('order'), $temp);
					if (is_array($temp = $temp['objects']) && sizeof($temp)) {

						$_cr2 = "id_data=$_id_data AND id_parent=$a";
						$DATA['objects'] = datafetch_array('db_list', array('table' => tb('data'), 'where' => $_cr2,	'order' => 'id_index', 'index' => 'id' ));
		
						foreach($temp as $id) {
							if ($id = (int)$id) {
								if ($DATA['objects'][$id]) {
									dataset('db_update', tb('data'), "id_index = ".$t++, "$_cr2 AND id=$id");
									unset($DATA['objects'][$id]);
								}
							}
						}
						
						if (sizeof($DATA['objects']))
							foreach($DATA['objects'] as $id => $v) 
								dataset('db_update', tb('data'), "id_index = ".$t++, "$_cr2 AND id=$id");
					}
					
					exit;	// ajax: no more processing needed
					break;
		}
			
			header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
			exit;
		}






	
		switch (action(array('editor_save', 'editor_save_enable', 'create_object'))) {
		
			case 'editor_save_enable':
			
				$DATA['object']['attr'] |= AA_VISIBLE;
			
			case 'editor_save':

				$_record = array('id_data' => $_id_meta, 'id_parent' => $a);
				$temp = array();
	
				foreach($DATA['langs'] as $lang) {	// store multilanguage
					foreach($_multilang as $name) {
						$t = postdata($name);
						$temp[] = array_merge($_record, array('id_parent' => $a, 'name' => $name, 'value' => $t[$lang['text']], 'assoc' => $lang['text']));
					}
				}

				foreach($_singlelang as $name)	// store language independent
					$temp[] = array_merge($_record, array('id_parent' => $a, 'name' => $name, 'value' => postdata($name), 'assoc' => ''));

				dataset('db_delete', tb('data'), "id_data=$_id_meta AND id_parent=$a");
				dataset('db_insert', tb('data'), $temp);

				$t = ($temp = trim($_POST['title'][LANG_DEF])) ? $temp : $DATA['object']['title'];
				dataset('db_update', tb('data'), array('name' => $t, 'attr' => $DATA['object']['attr']), "id=$a");
//debug($_FILES);				
//debug("$s[path]$a");
				takeImage('image', $s['w'], $s['h'], "$s[path]$a", '', '', '', MI_CUT_CENTER);

				header("Location: "._transformURL($CURRENT->url, array('remove_param' => $_param)));
				exit;
		
				break;
				
			case 'create_object':
//dbdebug();
//debug($a);
				if ($title = trim(postdata('title'))) {
		
					$max = ($m = datafetch('db_item', tb('data'), $_cr2, 'MAX(id_index) as m')) ? $m['m']+1 : 1;
		
					if ($id = datafetch('db_insert', tb('data'), array('id_data' => $_id_data, 'id_parent' => $a,
							'attr' => 0, 
							'name' => $title, 
							'id_index' => $max, 
						))) {
						
						dataset('db_insert', tb('data'), 
							array('id_data' => $_id_meta, 'id_parent' => $id, 'name' => 'title', 'value' => $title, 'assoc' => LANG_DEF)); 
						
						
						// redirect to edit page
						header("Location: "._transformURL($CURRENT->url, array('add_param' => "slide:$id")));
						exit;
					}
						// !!! else error on insert - handle it
				}
				
				break;
				
		}
	
	
	
		
		$DATA['object'] = array_merge($DATA['object'], datafetch_array('meta', $_id_meta, $a));

		foreach($DATA['langs'] as $lang)
			$DATA['object']['data'][$lang['text']] = datafetch_array('meta', $_id_meta, $a, $lang['text']);
			
		$DATA['object']['image'] = checkImage($s['path'], $a);
		
		$DATA['object']['categories'] = component_extract('categories');

		$_cr2 = "id_data=$_id_data AND id_parent=$a";
		$DATA['objects'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb('data'), 'where' => $_cr2,	'order' => 'id_index', 'index' => 'id' )));
		

		
//debug($DATA['object']);
//debug($DATA['objects']);

	}
	


	return false;
	
} else exit;